"""
给甜甜的生日歌 
"""
import time
import os
import winsound
from colorama import init, Fore, Back, Style

# 初始化colorama
init()


def play_birthday_song():
    # 播放生日快乐音乐的音符频率和持续时间
    # 调整频率范围在37-32767Hz之间
    notes = [440, 440, 494, 440, 587, 523,
             440, 440, 494, 440, 659, 587,
             440, 440, 880, 740, 587, 523, 494,
             784, 784, 740, 587, 659, 587]
    durations = [0.5, 0.5, 1, 1, 1, 2,
                 0.5, 0.5, 1, 1, 1, 2,
                 0.5, 0.5, 1, 1, 1, 1, 1,
                 0.5, 0.5, 1, 1, 1, 2]
    
    try:
        for note, duration in zip(notes, durations):
            # 确保持续时间至少为100毫秒
            duration_ms = max(100, int(duration * 500))
            winsound.Beep(note, duration_ms)
            time.sleep(0.1)
    except Exception as e:
        print(f"播放音乐时出错：{str(e)}")
        raise


def print_cake():
    cake = f"""
    {Fore.YELLOW}      🎂 🎂 🎂
    {Fore.RED}     .|..|..|..
    {Fore.YELLOW}   ############
    {Fore.LIGHTMAGENTA_EX}   ############
    {Fore.MAGENTA}   ############{Style.RESET_ALL}
    """
    return cake


def print_birthday_message():
    message = f"""
    {Fore.CYAN}★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
    {Fore.YELLOW}    祝甜甜生日快乐！
    {Fore.GREEN}    愿你永远快乐！
    {Fore.MAGENTA}    愿你梦想成真！
    {Fore.CYAN}★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★{Style.RESET_ALL}
    """
    return message


def main():
    os.system('cls')
    print("\n" * 2)
    
    # 显示生日蛋糕
    for _ in range(3):
        os.system('cls')
        print("\n" * 2)
        print(print_cake())
        time.sleep(0.5)
        os.system('cls')
        print("\n" * 3)
        time.sleep(0.5)
    
    print(print_cake())
    print(print_birthday_message())
    
    # 播放生日快乐歌
    try:
        play_birthday_song()
    except Exception as e:
        print(f"无法播放音乐：{str(e)}")
        print("但祝福依然在心中！")


if __name__ == "__main__":
    main()
